<?php


namespace App\Exceptions;

use Exception;
use Throwable;

class FailException extends Exception
{
    const HTTP_STATUS = 400;

    protected $data;

    protected $code;

    protected $meta;

    protected $status;

    public function __construct($data, $code = -1, $meta = [], $status = self::HTTP_STATUS)
    {
        $this->data = $data;
        $this->code = $code;
        $this->meta = $meta;
        $this->status = $status;
    }

    public function render()
    {
        $content = [];
        if (is_array($this->data)) {
            $content = $this->data;
        }
        if (is_string($this->data)) {
            $content = [
                'message' => $this->data,
                'status' => $this->code
            ];
        }
        if ($this->meta) {
            $content = array_add($content, 'meta', $this->meta);
        }

        return response($content, $this->status);
    }
}